package com.xiaoowuu.wherecanigo

import android.os.Bundle
import android.os.Parcelable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import com.amap.api.services.busline.BusLineItem
import kotlinx.android.synthetic.main.dialog_busline.*


/**
 * @author xiaoowuu
 */
class BusLineDialog : DialogFragment() {

    companion object {
        fun newInstance(busLineItems: MutableList<BusLineItem>): BusLineDialog {
            val dialog = BusLineDialog()
            val arguments = Bundle()
            arguments.putParcelableArrayList("busLine", busLineItems as ArrayList<out Parcelable>)
            dialog.arguments = arguments
            return dialog
        }
    }

    private val busLines: ArrayList<BusLineItem> by lazy {
        arguments?.apply {
            return@lazy getParcelableArrayList<BusLineItem>("busLine")
        }
        arrayListOf<BusLineItem>()
    }
    val adapter: BusLineAdapter by lazy {
        BusLineAdapter(busLines)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NO_FRAME, R.style.AppTheme)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        dialog.setCanceledOnTouchOutside(true)
        dialog.window?.let {
            it.setGravity(Gravity.BOTTOM)
            it.setBackgroundDrawableResource(android.R.color.transparent)
        }
        return inflater.inflate(R.layout.dialog_busline, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        view.setOnClickListener { dismiss() }
        rvBusLine.adapter = adapter
    }
}